<?php

namespace app\company\controller;

use app\admin\model\user\Category;
use app\common\controller\Api;
use app\common\controller\CompanyApi;
use think\Db;
use think\Request;


class Usercategory extends CompanyApi
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $user = $this->auth->getUser();
        if ($user->is_manager != 1) {
            $this->error('非法操作！');
        }
        self::$model = new Category();
    }

    /**
     * 分类列表
     */
    public function lists()
    {
        $community_id = $this->request->post('community_id');
        if (!$community_id) {
            $this->error('参数缺失！');
        }
        $where = [
            'community_id' => $community_id
        ];

        $lists = self::$model->field('id,title,image,status')->where($where)->order('sort asc')->select();
        if ($lists) {
            foreach ($lists as $val) {
                $val['image'] = make_full_url($val['image']);
            }
        }

        $this->success('分类列表', $lists);
    }

    /**
     * 新增分类
     */
    public function create()
    {
        $title = $this->request->post('title');
        $image = $this->request->post('image');
        $community_id = $this->request->post('community_id');
        if (!$title || !$community_id) {
            $this->error('参数缺失！');
        }

        $cdata = [
            'title' => $title,
            'community_id' => $community_id,
            'image' => $image,
        ];
        $ret = self::$model->create($cdata);
        if (!$ret) {
            $this->error('新增失败！');
        }
        $this->success('新增成功！');
    }
}
